package com.laoma.springbootalipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Component;

/**
 * @Author：xsp
 * @Description:
 * @name：PayTest
 * @Date：2024/10/28 19:31
 */
@Component
public class PayTest {


    public static void main(String[] args) {
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no ="155488";
        // 订单名称，必填
        String subject = "大黄";
        // 付款金额，必填
        String total_amount="655";
        // 商品描述，可空
        String body = "我是大黄黄";
        // 超时时间 可空
        String timeout_express="2m";
        // 销售产品码 必填
        String product_code="QUICK_WAP_WAY";
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayTemplate alipayTemplate = new AlipayTemplate();
        alipayTemplate.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjP0CXJpcqnf4VdJ5gEJ65rGPKLSDzFq/iZsfzPtl5mQNJNnXDlwfhBfew+79cqG/zGlJBHRYLpI16u3HIiQZF++WBbpZvFrZ6Hjt2e7qVWcgMUKqYvlufwmyjHDL9u0C+4BZ92KupI6h9NI5/H+OJX/ZG6VoT75NVKCpYUVMHVw7TuJOTB936ISj0YW5sIMujNImeEfqslkYIy8wFFvTuHEZW6RFmkDfJAIp3eKpoHRTIF3WDDKkozsgIiZKE4/BKhDsxGb0kTAbNd8D7yvmfUPmAT4e/Sxr5/CxPUf4sj802iEHjTRS8luvkV3FrvCg0Wohw010ZsHwNWcuWI+LAwIDAQAB");
        alipayTemplate.setAppId("9021000141660779");
        alipayTemplate.setGatewayUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayTemplate.setCharset("utf-8");
        alipayTemplate.setSignType("RSA2");
        alipayTemplate.setMerchantPrivateKey("MIIEvwIBADANBgkqhkiG9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QCFhv3/ul5HQiafFmwFEs43Ng9Ox+skwFr7JpmSNvIEk0GW41OknRFinlff6RiwpwoDeWWbTTve7P0fAsHVEbrzDzMN8YRWQ7U8hA2TxtD+CJUsaBqdvQzkt311Fu0n8Ij08ldPhDMuiZq3NUKbz14ssdgkrNqeNsLCpglY0rw51wKBgQC2ZW+7/A/Bm3TXhfK6prbhqKecSYNAD7ySTuA/ggOw3Fxv9uj0aVOtkEEthl+kpTEKdnd5DDAIvrf1KJ2dvQH9kGjxTpakOCwaJ+8Ve+CehzSibrcM9LRqzQ8JAor5nWLk8S8U07uimiOH2G0dK9F+V7yVcAMPCQ+N+8HxV/NtEQKBgQCdaook/qU5zDbyg/sPcuRORX8fkfoa9ZksiuXWCTVdusfi9h1bavehREvDPhJcuff5Vnk4GEvbqMsb5SesgGR3tbC3LitQnRL9nRvJju5BU+4yc883cXck815glOLboJjkyHFBC2DgZ8YDj1XRtqsH1USz1catkdowbGu62rPZrA==");
        alipayTemplate.setNotifyUrl("http://rcnjry.natappfree.cc");
        alipayTemplate.setReturnUrl("www.baidu.com");
        AlipayClient client = new DefaultAlipayClient(alipayTemplate.getGatewayUrl(), alipayTemplate.getAppId(), alipayTemplate.getMerchantPrivateKey(), "json", alipayTemplate.getCharset(), alipayTemplate.getAlipayPublicKey(),alipayTemplate.getSignType());
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(alipayTemplate.getNotifyUrl());
        // 设置同步地址
        alipay_request.setReturnUrl(alipayTemplate.getReturnUrl());

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            System.out.println("=============================================================");
            System.out.println(form);
//            response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
//            response.getWriter().write(form);//直接将完整的表单html输出到页面
//            response.getWriter().flush();
//            response.getWriter().close();
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
